package com.teamwork.module01.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Class;
import com.teamwork.common.entity.module01.Classroom;
import com.teamwork.common.result.Result;
import com.teamwork.module01.mapper.ClassMapper;
import com.teamwork.module01.service.ClassService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    /**
     * 根据名称获取班级信息
     * @param name
     * @return
     */
    @Override
    public Result getByName(String name) {

        LambdaQueryWrapper<Class> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Class::getClassName,name).or().like(Class::getClassName,name);

        Class one = this.getOne(wrapper);
        if(Objects.isNull(one)){
            return Result.failure("该班级不存在");
        }

        return Result.success(one);

    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getPage(Integer page, Integer size) {
        Page<Class> classPage = new Page<>(page,size);
        this.page(classPage);

        return Result.success(classPage);
    }

    /**
     * 添加或者修改
     * @param one
     * @return
     */
    @Override
    public Result InsertOrUpdate(Class one) {
        if(one.getId()!=null||!one.getId().equals("")){
            //修改
            boolean b = this.updateById(one);
            if(!b) return Result.failure("修改失败");
            return Result.success("修改成功");

        }
        //添加
        boolean sb = this.save(one);
        if(!sb) return Result.failure("添加失败");
        return Result.success("添加成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        return null;
    }

    /**
     * id数组批量删除
     * @param ids
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> ids) {
        for (Integer id : ids) {
            boolean b = this.removeById(id);
            if(!b) return Result.failure("删除失败");

        }
        return Result.success("删除成功");
    }
}
